package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddRessService;
import com.dongdongshop.service.OrderItemService;
import com.dongdongshop.service.OrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;

import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @Description
 * @ClassName OrderController
 * @Author RC
 * @date 2020.12.23 08:46
 */
@Controller
@RequestMapping("order")
public class OrderController {
    @Reference(timeout = 100000,check = false)
    private OrderService orderService;
    @Reference(timeout = 100000,check = false)
    private AddRessService addRessService;

    @Reference(timeout = 100000,check = false)
    private OrderItemService orderItemService ;


    public static final Logger logger = LoggerFactory.getLogger(OrderController.class);

    @RequestMapping("saveOrderList")
    @ResponseBody
    public DataResult saveOrderList(Long id,String userId,String sumPrice){
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
        try{
            logger.info("进入保存");
            Map<String,Object> map =orderService.saveOrderList(id,userId,sumPrice);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(map);
        } catch (Exception e) {
            logger.info("保存失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("getMyOrder")
    @ResponseBody
    public DataResult getMyOrder(){
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
        try{
            logger.info("进入查询订单");
            Subject subject = SecurityUtils.getSubject();
            TbUser user =(TbUser) subject.getPrincipal();
            List<OrderDTO> orderDTOList = orderService.getMyOrder(user.getUsername());
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(orderDTOList);
        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
    //退款
    @RequestMapping("refund")
    @ResponseBody
    public DataResult refund(Long id){
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
        try{
            logger.info("进入保存");
            Map<String,Object> map =orderService.refund(id);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(map);
        } catch (Exception e) {
            logger.info("保存失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
